//
//  TitleBanner.m
//  apeg
//
//  Created by suyibaishan on 16/5/6.
//  Copyright © 2016年 eone. All rights reserved.
//

#import "TitleBanner.h"

@implementation TitleBanner

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        self.showsHorizontalScrollIndicator = NO;
    }
    
    return self;
}

-(void)setTitleModelArray:(NSArray *)titleModelArray{
    
    _titleModelArray = titleModelArray;
    CGFloat titleLabelWidth = 0;
    if (titleModelArray.count <= 4) {
        titleLabelWidth  = [UIScreen mainScreen].bounds.size.width / titleModelArray.count;
        
    }else{
        
        titleLabelWidth  = [UIScreen mainScreen].bounds.size.width / 5;
    }
    
    for (int i = 0; i <titleModelArray.count; i++) {
        
        CGFloat titleLabelX = i * (titleLabelWidth+5);
        CGFloat titleLabelY = (self.frame.size.height - self.frame.size.height*2/3)/2;
        CGFloat titleLabelHight = self.frame.size.height;
        
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.tag = i;
        titleLabel.clipsToBounds = YES;
        titleLabel.layer.cornerRadius = 5;
        titleLabel.font = [UIFont systemFontOfSize:13];
        titleLabel.frame = CGRectMake(8+titleLabelX, titleLabelY, titleLabelWidth, titleLabelHight*2/3);
        titleLabel.text = titleModelArray[i];
        titleLabel.textColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.userInteractionEnabled = YES;
        titleLabel.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
        [titleLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleLabelClick:)]];
        if (i == 0) {
            titleLabel.textColor = [UIColor whiteColor];
            titleLabel.backgroundColor = [UIColor colorWithRed:0.98 green:0.24 blue:0.47 alpha:1];
        }
        
        [self addSubview:titleLabel];
    }
    
    self.contentSize = CGSizeMake(8+titleModelArray.count *(titleLabelWidth+5), self.frame.size.height);
}

-(void)titleLabelClick:(UITapGestureRecognizer *)recognizer{
    
    UILabel *label = (UILabel *)recognizer.view;
//    mAppDelegate.bannerIndex = label.tag;
    
    for (UILabel *lab in self.subviews) {
        if ([lab isKindOfClass:[UILabel class]]) {
            lab.textColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1];
            lab.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
        }
    }
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor colorWithRed:0.98 green:0.24 blue:0.47 alpha:1];
        
    if ([self.titledelegate respondsToSelector:@selector(titleBanner:didSelectRowAtIndex:)]) {
        
        [self.titledelegate titleBanner:self didSelectRowAtIndex:label.tag];
    }
}

@end
